package logic

import (
	"context"
	"fmt"
	"product666/model/mysql"
	"product666/model/redis"

	"product666/userrpc/internal/svc"
	"product666/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LikeLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLikeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeLogic {
	return &LikeLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LikeLogic) Like(in *userrpc.LikeRequest) (*userrpc.LikeResponse, error) {
	// todo: add your logic here and delete this line
	var findpro mysql.Product
	mysql.DB.Where("id=?", in.Id).Find(&findpro)
	if findpro.ID == 0 {
		return nil, fmt.Errorf("没有找到该商品")
	}
	redis.Client.ZIncrBy("like", 1, findpro.Title)
	return &userrpc.LikeResponse{}, nil
}
